/*
 * Copyright © 2012 Nokia Corporation. All rights reserved.
 * Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation. 
 * Oracle and Java are trademarks or registered trademarks of Oracle and/or its
 * affiliates. Other product and company names mentioned herein may be trademarks
 * or trade names of their respective owners. 
 * See LICENSE.TXT for license information.
 */
package com.nokia.example.miniapp.form;

import com.nokia.example.miniapp.form.ButtonGroupItem.ButtonGroupListener;
import com.nokia.uihelpers.Button;
import com.nokia.uihelpers.Compatibility;
import com.nokia.uihelpers.Slider;
import com.nokia.uihelpers.Slider.SliderListener;
import com.nokia.uihelpers.Switch;
import com.nokia.uihelpers.Switch.SwitchListener;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.midlet.MIDlet;

/**
 * Demonstrates the usage of custom form elements
 */
public class CustomView
    extends FormExampleView
    implements ButtonGroupListener, SliderListener, SwitchListener {

    private int selectedIndex = 0;
    private ButtonGroupItem segmentedButton;
    private ButtonGroupItem multiButtonLine;
    private Slider slider;
    private Switch aSwitch;
    private MIDlet parent;

    public CustomView(int foregroundColor, int backgroundColor,
            CommandListener commandListener, MIDlet parent, String title) {
        super(title, commandListener);
        this.parent = parent;
        
        // The slider
        slider = new Slider(1, 30,
                Compatibility.toLowerCaseIfFT("Slider"), this);
        Compatibility.setObjectTrait(slider,
            "nokia.ui.s40.item.direct_touch", Boolean.TRUE);
        slider.addListener(this);
        this.append(slider);
        
        // The switch
        aSwitch = new Switch(Compatibility.toLowerCaseIfFT("Switch"),
                Compatibility.toLowerCaseIfFT("Value is on"),
                Compatibility.toLowerCaseIfFT("Value is off"),
                this);
        Compatibility.setObjectTrait(aSwitch,
            "nokia.ui.s40.item.direct_touch", Boolean.TRUE);
        this.append(aSwitch);
        
        // The segmented button
        segmentedButton = new ButtonGroupItem(
            Compatibility.toLowerCaseIfFT("Segmented button"),
            new String[]{"YES", "NO"},
            foregroundColor,
            backgroundColor,
            Button.SEGMENTED,
            this);
        Compatibility.setObjectTrait(segmentedButton,
            "nokia.ui.s40.item.direct_touch", Boolean.TRUE);
        segmentedButton.addListener(this);
        this.append(segmentedButton);
        
        // 2 buttons in a line
        multiButtonLine = new ButtonGroupItem(
            Compatibility.toLowerCaseIfFT("2 buttons in a line"),
            new String[]{"YES", "NO"},
            foregroundColor,
            backgroundColor,
            Button.REGULAR,
            this);
        Compatibility.setObjectTrait(multiButtonLine,
            "nokia.ui.s40.item.direct_touch", Boolean.TRUE);
        multiButtonLine.addListener(this);
        this.append(multiButtonLine);
    }

    /**
     * Handler for the events generated by the ButtonGroup
     * @param buttonGroup
     * @param buttonIndex 
     */
    public void clicked(ButtonGroupItem buttonGroup, int buttonIndex) {
        if (buttonGroup == multiButtonLine) {
            String alertText = "You pressed button number " + 
                    (buttonIndex + 1) +
                    " from 2 buttons in a line";
            Alert alert = new Alert("");
            alert.setString(alertText);
            alert.setTimeout(3000);

            Display.getDisplay(parent).setCurrent(alert);
        }
    }

    protected boolean confirmChanges() {
        return selectedIndex != segmentedButton.getActive();
    }

    protected void storeCurrentValues() {
        selectedIndex = segmentedButton.getActive();
    }

    protected void cancelChanges() {
        segmentedButton.setActive(selectedIndex);
    }

    public void valueChanged(Slider slider, int value) {
    }

    public void valueChanged(Switch aSwitch, boolean value) {
    }
}
